import { forEach } from '/@/utils/helper/treeHelper';
import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import { rules} from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import {list as listRule} from '/@/views/attendance/attruledeptrelation/AttRuleDeptRelation.api';
import {list as listTimePeriod} from '/@/views/attendance/timeperiod/AttTimePeriod.api'
import {list as listGroup} from '/@/views/baseinfo/persongroup/SsPersonGroup.api'
import {list as listAttSchedule} from './AttSchedule.api';


import {reactive,ref,toRaw}  from 'vue';

//列表数据attScheduleList
export const columns: BasicColumn[] = [
  {
    title: '部门',
    align:"center",
    dataIndex: 'departmentId_dictText'
  },
  {
   title: '组',
   align:"center",
   dataIndex: 'personGroupId_dictText'
  },
  {
    title: '姓名',
    align:"center",
    dataIndex: 'personId_dictText'
  },
  {
   title: '日期',
   align:"center",
   dataIndex: 'workDate',
   customRender:({text}) =>{
     return !text?"":(text.length>10?text.substr(0,10):text)
   },
  },
   {
    title: '时段',
    align:"center",
    dataIndex: 'timePeriodIdDetail'
   },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '部门',
    field: 'departmentId',
    component: 'JSelectDept',
    componentProps:{
      // checkStrictly:true,
      multiple:false,
      params:{
        businessCode:1001
      }
    }
  },
  {
    label: "姓名",
    field: 'personName',
    component: 'Input',
    //colProps: {span: 6},
 },
 {
  label: "日期",
  field: "workDate",
  component: 'DatePicker',
  componentProps:{
    showToday:true,
    valueFormat: 'YYYY-MM-DD',
  }
}
];
const tpoptions = reactive([]);
const goptions = reactive([]);
const originalClass = reactive([]);
const targetClass = reactive([]);
const changeTargetArr = ref([]);
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '部门',
    field: 'departmentId',
    component: 'JSelectDept',
    componentProps:  ({formModel})=>
    {

     return {
        checkStrictly:true,
          multiple:false,
       onChange:async (e)=>{
          console.log(e);
         goptions.splice(0, goptions.length)
         tpoptions.splice(0, tpoptions.length)
          if(e.length==0) {
            return;
          }

         let re = await listRule({departmentId:e[0],delFlag:"0",status:1});
         console.log(re);
         if(re?.records?.length>0) {
           let re1 = await listTimePeriod({ruleId: re.records[0].attRuleId});
           console.log(re1);

           for(let i = 0;i<re1.records.length;i++){
             let option = {};
             option.value = re1.records[i].id;
             option.label = re1.records[i].name;
             tpoptions.push(option);
           }
           console.log(formModel)
         }
         let re3 = await listGroup({departmentId:e[0]});
         if(re3?.records?.length>0){
           console.log(re3)

           for(let i = 0;i<re3.records.length;i++){
             let option = {};
             option.value=re3.records[i].id;
             option.label = re3.records[i].name;
             goptions.push(option);
           }
         }

       }
      }
    }
  },
  {
    label: '时段',
    field: 'timePeriodId',
    component: 'Select',
    componentProps:{
      options:tpoptions,
    },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入时段id!'},
          ];
     },
  },
  {
    label: '组',
    field: 'personGroupId',
    component: 'Select',
    componentProps:{
      options:goptions,
    },
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入组!'},
          ];
     },
  },
  {
    label: '日期',
    field: 'workDate',
    component: 'DatePicker',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入日期!'},
          ];
     },
  },
	// TODO 主键隐藏字段，目前写死为ID
	{
	  label: '',
	  field: 'id',
	  component: 'Input',
	  show: false
	},
];


export const initFormSchema: FormSchema[] = [
 
  {
    label: '部门',
    field: 'deptId',
    component: 'JSelectDept',
    slot: 'deptIdSlot'
  },
  
  {
    label: '分组',
    field: 'groupId',
    required: true,
    component: 'JDictSelectTag',
    slot: 'groupSlot'
  },
  {
    label: '班次',
    field: 'timePeriodId',
    required: true,
    component: 'JDictSelectTag',
    slot: 'timePeriodSlot'
  },
 {
    label: '选择人员',
    field: 'personIds',
    component: 'Input',
    slot: 'personIdSlot'
  },
  /*
  {
    label: '设置区间',
    field: 'timeRang',
    component: 'RadioButtonGroup',
    componentProps:{
    },
    slot:'timeRangSlot'
  },
  */
  
  {
	  label: "开始日期",
	  field: "sdate",
      required: true,
	  component: 'DatePicker',
	  componentProps:{
		showToday:true,
		valueFormat: 'YYYY-MM-DD',
	  }
  },
  {
	  label: "截止日期",
	  field: "edate",
      required: true,
	  component: 'DatePicker',
	  componentProps:{
		showToday:true,
		valueFormat: 'YYYY-MM-DD',
	  }
  },
  {
    label: '是否覆盖已排班',
    field: 'autorewrite',
    component: 'RadioButtonGroup',
    componentProps:{
      options:[
      {label: '否',value :false},
      {label: '是',value :true}
      ],
    },
    defaultValue: false
  },
];

export const changeFormSchema: FormSchema[] = [
  {
    field: '',
    component: 'Divider',
    label: '申请人信息',
    colProps: {span: 24},
  },
  {
    label: '申请人日期',
    field: 'originaDate',
    component: 'DatePicker',
    // slot: 'workDateSlot',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入日期!'},
          ];
     },
     dynamicDisabled: true,
     componentProps:({formModel})=>{
        return {
          onChange:(e)=>{
            let data = {
              id: '',
              workDate: e,
              personName: toRaw(formModel).originalName
            }
            if(e && data.personName ){
              listAttSchedule(data).then(res=>{
                  if(res.records.length){
                    let item = res.records[0]
                    formModel.timePeriodName = item.timePeriodIdDetail
                    formModel.originalId = item.id
                    console.log(formModel);
                  }else{
                    formModel.timePeriodName = ''
                    formModel.originalId = ''
                  }
              })
            }
           
          },
          valueFormat: 'YYYY-MM-DD'
        }
     }
  },
  {
    label: '申请人姓名',
    field: 'originalName',
    component: 'Input',
    // slot: 'originalSlot',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, },
          ];
     },
     dynamicDisabled: true,
     componentProps:({formModel})=>{
      return {
        onChange:(e)=>{
          console.log(e.target.value);
          let data = {
            id: '',
            workDate: toRaw(formModel).originaDate,
            personFullName: e.target.value
          }
          console.log(data);
          if(e && data.personFullName ){
            listAttSchedule(data).then(res=>{
                if(res.records.length){
                  let item = res.records[0]
                  formModel.timePeriodName1 = item.timePeriodIdDetail
                  formModel.originalId = item.id
                  console.log(formModel);
                }else{
                  formModel.timePeriodName1 = ''
                  formModel.originalId = ''
                }
            })
          }
         
        },
      }
   }
  },
  {
    label: '申请人班次',
    field: 'timePeriodName1',
    component: 'Input',
    // slot: 'originalSlot',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, },
          ];
     },
     dynamicDisabled: true
  },
  {
    label: '申请人班次id',
    field: 'originalId',
    component: 'Input',
    // slot: 'originalSlot',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, },
          ];
     },
     show:false
  },
  {
    field: '',
    component: 'Divider',
    label: '换班人信息',
    colProps: {span: 24},
  },
  {
    label: '换班人日期',
    field: 'targetDate',
    component: 'DatePicker',
    // slot: 'workDateSlot',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, message: '请输入日期!'},
          ];
     },
    componentProps:({formModel})=>{
      return {
        onChange:(e)=>{
          let data = {
            id: toRaw(formModel).originalId,
            workDate: e,
            personFullName: toRaw(formModel).targetName
          }
          if(e && data.personFullName ){
            listAttSchedule(data).then(res=>{
                let item = res.records[0]
                formModel.timePeriodName2 = item.timePeriodIdDetail
                formModel.targetId = item.id
                console.log(formModel);
                changeTargetArr.value = []
                res.records.forEach(item2=>{
                  let obj = {
                    label: item2.timePeriodIdDetail,
                    value: item2.id,
                  }
                  changeTargetArr.value.push(obj)
                })
            })
          }
         
        },
        valueFormat: 'YYYY-MM-DD'
      }
   }
    
  },
  {
    label: '换班人姓名',
    field: 'targetName',
    component: 'Input',
    // slot: 'targetSlot',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, },
          ];
     },
     componentProps:({formModel})=>{
      return {
        onChange:(e)=>{
          let data = {
            id: toRaw(formModel).originalId,
            workDate: toRaw(formModel).targetDate,
            personFullName:  e.target.value
          }
          if(data.workDate && data.personFullName ){
            listAttSchedule(data).then(res=>{
                if(res.records.length){
                  let item = res.records[0]
                  formModel.timePeriodName2 = item.timePeriodIdDetail
                  formModel.targetId = item.id
                  console.log(formModel);
                  changeTargetArr.value = []
                  res.records.forEach(item2=>{
                    let obj = {
                      label: item2.timePeriodIdDetail,
                      value: item2.id,
                    }
                    changeTargetArr.value.push(obj)
                  })

                }else{
                  formModel.timePeriodName2 = ''
                  formModel.targetId = ''
                }
            })
          }
         
        },
      }
   }
  },
  {
    label: '换班人班次',
    field: 'timePeriodName2',
    // component: 'Input',
    component: 'Select',
    componentProps:{
      options:changeTargetArr,
    },
    // slot: 'targetSlot',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, },
          ];
     },
    //  dynamicDisabled: true
  },
  {
    label: '换班人班次',
    field: 'targetId',
    component: 'Input',
    // slot: 'targetSlot',
    dynamicRules: ({model,schema}) => {
          return [
                 { required: true, },
          ];
     },
     show:false
  },
]


// 高级查询数据
export const superQuerySchema = {
  timePeriodId: {title: '时段id',order: 0,view: 'text', type: 'string',},
  personGroupId: {title: '组',order: 1,view: 'text', type: 'string',},
  workDate: {title: '日期',order: 2,view: 'date', type: 'string',},
};

/**
* 流程表单调用这个方法获取formSchema
* @param param
*/
export function getBpmFormSchema(_formData): FormSchema[]{
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
